<template>
    <div class="login-form">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <h1>安踏后台管理系统</h1>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" type="password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data(){
        return {
            /* 表单信息 */
            ruleForm:{
                username:'',
                password:''
            },
            /* 正则验证 */
            rules:{
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        submitForm(formName) {
            this.$refs[formName].validate(async(valid) => {
                /* 全部通过验证则发送请求 */
                if(valid){
                    const {data} = await this.$request({
                        url:'/user/login',
                        method:'post',
                        data:{
                            ...this.ruleForm
                        }
                    })
                    /* flag为true时，验证通过 */
                    if(data.flag){
                        /* 发送请求验证token */
                        let checkToken = await this.$request({
                            url:`/user/info/${data.data.token}`
                        })
                        /* token通过时，跳转登录页面并将用户信息存入localstorage */
                        if(checkToken.data.flag){
                            this.$notify({
                                title: '成功',
                                message: '登录成功！',
                                type: 'success'
                            });
                            /* 先存储 */
                            localStorage.setItem('admin',checkToken.data.data.name);
                            localStorage.setItem('token',data.data.token);
                            /* 后跳转 */
                            this.$router.push('/');
                        }else{
                            this.$notify.error({
                                title: '失败',
                                message: '登录失败！'
                            });
                        }
                    }
                }else{
                    this.$notify.error({
                        title: '失败',
                        message: '请重新填写正确帐密！'
                    });
                    return false;
                }
            });
        }
    }
}
</script>

<style lang='scss' scoped>
    .login-form{
        position: absolute;
        overflow: hidden;
        width: 100%;
        height: 100%;
        background: url('../../assets/img/login2.jpg');
        .demo-ruleForm{
            width: 360px;
            padding: 30px 70px 30px 30px;
            background: rgba(#fff, 0.8);
            margin: 180px auto;
            border-radius: 30px;
            h1{
                font-size: 30px;
                margin-bottom: 20px;
                text-align: center;
            }
        }
    }
</style>